package com.goexchange.controller;

import java.io.IOException;
import java.util.List;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.goexchange.data.PMF;
import com.goexchange.data.Subscriber;

@SuppressWarnings("serial")
public class ViewSubscriber extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		doPost(req, resp);
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Subscriber.class);
		query.setOrdering("date asc");
		try{
			@SuppressWarnings("unchecked")
			List<Subscriber> results = (List<Subscriber>)query.execute();
			if(!results.isEmpty()){
				req.setAttribute("results", results);
			}
			String url = "/displaySub.jsp";
			RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(url);
			dispatcher.forward(req, resp);
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			query.closeAll();
		}
	}
}
